﻿using HookupAppForm.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormSysEdit : Form
    {
        private readonly string url = "api/services/app/HKP_System/";
        private APIClient Client { get; set; }

        private HKP_SystemDto Item { get; set; }
        public FormSysEdit(HKP_SystemDto _item, APIClient _client)
        {            
            InitializeComponent();
            Item = _item;
            Client = _client;
        }
        private void LoadData()
        {
            string act = string.Format(url+"GetList?maxResultCount={0}&skipCount={1}", 999, 0);
            var result = Client.Get(act);
            ItemsDto<HKP_SystemDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_SystemDto>>(result);
            dataGridViewM.DataSource = res.result.items;
            FromSetting[] fromSettings = new FromSetting[3];
            fromSettings[0] = new FromSetting { FieldDisplay = "系统编号" ,FieldDisplayIndex=3};
            fromSettings[1] = new FromSetting { FieldDisplay = "系统名称" ,FieldDisplayIndex=2};
            fromSettings[2] = new FromSetting { FieldDisplay = "系统类型",FieldDisplayIndex=1 };
            Utils.SetHeader(dataGridViewM, fromSettings);
        }
        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string act = Item == null ? "Create" : "Update";
            string result = string.Empty;
            if (Item != null)
            {
                Item.SysNo = SysNoBox.Text;
                Item.SysType = ucDrpSysCls.selectedText;
                Item.SysName = SysNameBox.Text;
                string jsons = JsonConvert.SerializeObject(Item);
                result = Client.Put(url + act, jsons);
            }
            else
            {
                CreateHKP_SystemDto obj = new CreateHKP_SystemDto();
                obj.SysNo = SysNoBox.Text;
                obj.SysType = ucDrpSysCls.selectedText;
                obj.SysName = SysNameBox.Text;
                obj.Floor = "0";
                string jsons = JsonConvert.SerializeObject(obj);
                result = Client.Post(url + act, jsons);
            }
            if (Utils.ExceptionHandler(result) == null)
            {
                Utils.SysMsg("保存成功！");
               // DialogResult = DialogResult.OK;
                LoadData();
            }
        }

        private void FormSysEdit_Load(object sender, EventArgs e)
        {
            ucDrpSysCls.Init("System", Client.RemoteHost);
            LoadData();
            if (Item != null)
            {
                SysNoBox.Text = Item.SysNo;
                SysNameBox.Text = Item.SysName;
                ucDrpSysCls.SetText(Item.SysType);
            }           
        }

        private void delToolStripButton_Click(object sender, EventArgs e)
        {


            if (MessageBox.Show("确定删除？", "系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (Item != null)
                {
                    //string act = string.Format(url + "Delete?Id={0}", Item.Id);
                    //string result = Client.Delete(act);
                    //if (Utils.ExceptionHandler(result) == null)
                    //{
                    //    //DialogResult = DialogResult.OK;
                    //    LoadData();
                    //}
                }
            }
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            if (Item != null)
            {
                string act = url + "Create";
                Item.SysNo += "Copy";
                string jsons = JsonConvert.SerializeObject(Item);
                string result = Client.Post(act, jsons);
                if (Utils.ExceptionHandler(result) == null)
                {
                    //DialogResult = DialogResult.OK;
                    LoadData();
                }
            }
        }

        private void DataGridViewM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Item = dataGridViewM.CurrentRow.DataBoundItem as HKP_SystemDto;
            if (Item != null)
            {
                SysNoBox.Text = Item.SysNo;
                SysNameBox.Text = Item.SysName;
                ucDrpSysCls.SetText(Item.SysType);
            }
        }
    }
}
